/*
 *
 * -------------------------------------------------------------------------------------------------
 *                            COPYRIGHT(C) BOCO HEALTH 2016
 * -------------------------------------------------------------------------------------------------
 *
 * =================================================================================================
 *  *HISTORY
 *
 *      Tag                Date            Author           Description
 * =================================================================================================
 *      created            2017/1/17      caodailu        create
 * =================================================================================================
 *
 *
 */

package com.boco.familyuser.view.main;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.boco.familyuser.R;

import java.util.List;

public class SimplePagerAdapter extends FragmentPagerAdapter {

    private Context context;
    private List<Fragment> listFragments;
    private String titles[] = {"首页", "消息", "圈子", "我的"};
//    private int[] icons = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable
//            .home_tab3, R.drawable.home_tab4};

    public SimplePagerAdapter(Context context, FragmentManager fm,
                              List<Fragment> al) {
        super(fm);
        this.context = context;
        listFragments = al;
    }

    public SimplePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return listFragments.get(position);
    }

    @Override
    public int getCount() {
        return listFragments.size();
    }

    /**
     * 防止频繁的销毁视图，设置ViewPager.setOffscreenPageLimit(2)<br/>
     * 或者重写PagerAdaper的destroyItem方法为空,<br/>
     * 即注释掉调用父类方法即可
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_tab, null);
        TextView title = (TextView) view.findViewById(R.id.home_tab_tv);
        if (position == 0) {
            title.setTextColor(ContextCompat.getColor(context, R.color.color_22c064));
        }
//        ImageView icon = (ImageView) view.findViewById(R.id.home_tab_icon);
        title.setText(titles[position]);
//        icon.setBackgroundResource(icons[position]);
        return view;
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
